﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using DAL;
using Common;

namespace BLL
{
    public class CategoryBLL
    {

        private CategoryDAL dal = new CategoryDAL();

        /// <summary>
        /// 获取所有标签
        /// </summary>
        /// <returns></returns>
        public List<Category> GetList()
        {
            return dal.GetList();
        }

        /// <summary>
        /// 获取分类名称
        /// </summary>
        /// <param name="cateid">[1][2]</param>
        /// <returns></returns>
        public string GetCateName(string cateid)
        {
            string id = cateid.Replace("【", "").Replace("】", ",").TrimEnd(',');
            StringBuilder str = new StringBuilder();
            List<dynamic> dt = dal.GetList("cate_id,cate_name", " and cate_id in (" + id + ")");
            
            for (int i = 0; i < dt.Count; i++)
            {
                if(str.Length>0)
                {
                    str.Append(",");
                }
                str.AppendFormat("<a href=\"/Blog/Cate/{0}\" >{0}</a>",Utils.ObjectToStr(dt[i].cate_name));
            }
            return str.ToString();
        }

        /// <summary>
        /// 获取一条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Category GetModel(string id)
        {
            return dal.GetModel(id);
        }

        /// <summary>
        /// 分类名称获取分类id
        /// </summary>
        /// <param name="catename"></param>
        /// <returns></returns>
        public int GetCateID(string catename)
        {
            return dal.GetCateID(catename);
        }

        /// <summary>
        /// 获取自定义数据
        /// </summary>
        /// <param name="columns">列名</param>
        /// <param name="where">条件 </param>
        /// <returns></returns>
        public List<dynamic> GetList(string column, string where)
        {
            return dal.GetList(column, where);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnMessages Add(Category model)
        {
            ReturnMessages rm = new ReturnMessages();
            try
            {
                rm = ValidateName(model);
                if (rm.code!=200)
                {
                    return rm;
                }
                int count = dal.Add(model);
                if (count > 0)
                {
                    rm.code = 200;
                    rm.msg = "添加成功";
                }
                else
                {
                    rm.code = 400;
                    rm.msg = "添加失败,影响行数为0";
                }
            }
            catch (Exception ex)
            {
                rm.code = 500;
                rm.msg = "服务器错误！";
                LogHelper.Error(this.GetType().FullName, ex.ToString());
            }
            return rm;

        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnMessages Update(Category model)
        {
            ReturnMessages rm = new ReturnMessages();
            try
            {
                rm = ValidateName(model);
                if (rm.code!=200)
                {
                    return rm;
                }
                int count = dal.Update(model);
                if (count > 0)
                {
                    rm.code = 200;
                    rm.msg = "更新成功";
                }
                else
                {
                    rm.code = 400;
                    rm.msg = "更新失败,影响行数为0";
                }
            }
            catch (Exception ex)
            {
                rm.code = 500;
                rm.msg = "服务器错误！";
                LogHelper.Error(this.GetType().FullName, ex.ToString());
            }
            return rm;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ReturnMessages delete(int id)
        {
            ReturnMessages rm = new ReturnMessages();
            try
            {
                int count = dal.delete(id);
                if (count > 0)
                {
                    rm.code = 200;
                    rm.msg = "删除成功";
                }
                else
                {
                    rm.code = 400;
                    rm.msg = "删除失败,影响行数为0";
                }
            }
            catch (Exception ex)
            {
                rm.code = 500;
                rm.msg = "服务器错误！";
                LogHelper.Error(this.GetType().FullName, ex.ToString());
            }
            return rm;
        }

        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public ReturnMessages ValidateName(Category m)
        {
            ReturnMessages rm = new ReturnMessages();
            List<dynamic> dt = GetList("*", " and cate_name='" + m.cate_name + "' and cate_id!=" + m.cate_id);
            if (dt.Count > 0)
            {
                rm.code = 400;
                rm.msg = "分类名称已存在！";
            }
            else
            {
                rm.code = 200;
                rm.msg = "";
            }
            return rm;
        }

        /// <summary>
        /// 更新分类文章数量
        /// </summary>
        /// <returns></returns>
        public void UpdateCateCount()
        {
            try
            {
                List<dynamic> dt = dal.GetList("cate_id", "");
                for (int i = 0; i < dt.Count; i++)
                {
                    dal.UpdateCateCount(Utils.ObjToInt(dt[i].cate_id,0));
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(this.GetType().FullName, ex.ToString());
            } 
        }

    }
}
